package bb.lanxing.nav;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.baidu.mapapi.model.LatLng;
import com.garmin.fit.MonitoringReader;

import org.apache.commons.cli.HelpFormatter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.model.json.Route;
import bb.lanxing.model.json.RouteLeg;
import bb.lanxing.model.json.RoutePoint;
import bb.lanxing.model.json.RoutePolyline;
import bb.lanxing.model.json.RouteStep;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.DateUtil;
import bb.lanxing.util.JsonUtil;
import bb.lanxing.util.PolyUtil;
import bb.lanxing.util.map.SphericalUtil;

public class BaiduNavConvert {
    public static Route parseRoute(JSONObject jSONObject) {
        Route route = new Route();
        route.setPlanSource(Route.PlanSource.Baidu);
        if (jSONObject.has("result")) {
            try {
                JSONObject jSONObject2 = jSONObject.getJSONObject("result");
                if (jSONObject2.has("routes")) {
                    JSONArray jSONArray = jSONObject2.getJSONArray("routes");
                    if (jSONArray.length() > 0) {
                        JSONObject jSONObject3 = jSONArray.getJSONObject(0);
                        RouteLeg routeLeg = new RouteLeg();
                        int integerValue = JsonUtil.getIntegerValue(MonitoringReader.DISTANCE_STRING, jSONObject3);
                        int integerValue2 = JsonUtil.getIntegerValue("duration", jSONObject3);
                        RouteStep.IntValuePair intValuePair = new RouteStep.IntValuePair();
                        intValuePair.setValue(integerValue);
                        intValuePair.setText(SphericalUtil.formatDistance(integerValue));
                        routeLeg.setDistance(intValuePair);
                        RouteStep.IntValuePair intValuePair2 = new RouteStep.IntValuePair();
                        intValuePair2.setValue(integerValue2);
                        intValuePair2.setText(DateUtil.formatTime(integerValue2));
                        routeLeg.setDuration(intValuePair2);
                        LatLng baidu2Common = BiCiCoorConverter.baidu2Common(((RoutePoint) JSON.parseObject(JsonUtil.getStringValue("originLocation", jSONObject3), RoutePoint.class)).toLatLng());
                        LatLng baidu2Common2 = BiCiCoorConverter.baidu2Common(((RoutePoint) JSON.parseObject(JsonUtil.getStringValue("destinationLocation", jSONObject3), RoutePoint.class)).toLatLng());
                        routeLeg.setStartLocation(new RoutePoint(baidu2Common));
                        routeLeg.setEndLocation(new RoutePoint(baidu2Common2));
                        JSONArray jSONArray2 = jSONObject3.getJSONArray("steps");
                        ArrayList arrayList = new ArrayList();
                        if (jSONArray2.length() > 0) {
                            RouteStep[] routeStepArr = new RouteStep[jSONArray2.length()];
                            for (int i = 0; i < jSONArray2.length(); i++) {
                                RouteStep parseStep = parseStep(jSONArray2.getJSONObject(i), arrayList);
                                if (i > 0) {
                                    NavDirectionHelper.calDirection(routeStepArr[i - 1], parseStep);
                                }
                                routeStepArr[i] = parseStep;
                            }
                            routeLeg.setSteps(routeStepArr);
                        }
                        route.setOverviewPolyline(new RoutePolyline(PolyUtil.encode(arrayList)));
                        route.setLegs(new RouteLeg[]{routeLeg});
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return route;
    }

    private static RouteStep parseStep(JSONObject jSONObject, List<LatLng> list) {
        RouteStep routeStep = new RouteStep();
        int integerValue = JsonUtil.getIntegerValue(MonitoringReader.DISTANCE_STRING, jSONObject);
        int integerValue2 = JsonUtil.getIntegerValue("duration", jSONObject);
        RouteStep.IntValuePair intValuePair = new RouteStep.IntValuePair();
        intValuePair.setValue(integerValue);
        intValuePair.setText(SphericalUtil.formatDistance(integerValue));
        routeStep.setDistance(intValuePair);
        RouteStep.IntValuePair intValuePair2 = new RouteStep.IntValuePair();
        intValuePair2.setValue(integerValue2);
        intValuePair2.setText(DateUtil.formatTime(integerValue2));
        routeStep.setDuration(intValuePair2);
        routeStep.setHtmlInstruction(JsonUtil.getStringValue("instructions", jSONObject));
        String stringValue = JsonUtil.getStringValue("stepOriginInstruction", jSONObject);
        if (!TextUtils.isEmpty(stringValue)) {
            int indexOf = stringValue.indexOf(HelpFormatter.DEFAULT_OPT_PREFIX);
            if (indexOf != -1) {
                stringValue = stringValue.substring(0, indexOf).trim();
            }
            routeStep.setStartInstruction(stringValue);
        }
        String stringValue2 = JsonUtil.getStringValue("stepDestinationInstruction", jSONObject);
        if (!TextUtils.isEmpty(stringValue2)) {
            int indexOf2 = stringValue2.indexOf(HelpFormatter.DEFAULT_OPT_PREFIX);
            if (indexOf2 != -1) {
                stringValue2 = stringValue2.substring(0, indexOf2).trim();
            }
            routeStep.setEndInstruction(stringValue2);
        }
        LatLng latLng = ((RoutePoint) JSON.parseObject(JsonUtil.getStringValue("stepOriginLocation", jSONObject), RoutePoint.class)).toLatLng();
        LatLng baidu2Common = BiCiCoorConverter.baidu2Common(latLng);
        LatLng baidu2Common2 = BiCiCoorConverter.baidu2Common(((RoutePoint) JSON.parseObject(JsonUtil.getStringValue("stepDestinationLocation", jSONObject), RoutePoint.class)).toLatLng());
        routeStep.setStartLoc(new RoutePoint(baidu2Common));
        routeStep.setEndLoc(new RoutePoint(baidu2Common2));
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        arrayList.add(baidu2Common);
        arrayList2.add(BiCiCoorConverter.baidu2Earth(latLng));
        String stringValue3 = JsonUtil.getStringValue("path", jSONObject);
        if (!TextUtils.isEmpty(stringValue3)) {
            String[] split = stringValue3.split(";");
            if (split.length > 0) {
                for (String str : split) {
                    String[] split2 = str.split(",");
                    LatLng latLng2 = new LatLng(Double.parseDouble(split2[1]), Double.parseDouble(split2[0]));
                    arrayList.add(BiCiCoorConverter.baidu2Common(latLng2));
                    arrayList2.add(BiCiCoorConverter.baidu2Earth(latLng2));
                }
            }
        }
        RoutePolyline routePolyline = new RoutePolyline(PolyUtil.encode(arrayList));
        if (list != null) {
            list.addAll(arrayList);
        }
        routeStep.setPolyline(routePolyline);
        routeStep.setPolyPath(arrayList2);
        return routeStep;
    }

    private static void convertInstructions(JSONObject jSONObject, Boolean bool) throws JSONException {
        String replaceAll;
        String replaceAll2;
        String replaceAll3;
        if (jSONObject.has("instructions")) {
            String string = jSONObject.getString("instructions");
            if (bool) {
                replaceAll3 = string.replaceAll("终点", "下一途经点");
            } else {
                replaceAll3 = string.replaceAll("起点", "途经点");
            }
            jSONObject.put("instructions", replaceAll3);
        }
        if (jSONObject.has("stepOriginInstruction")) {
            String string2 = jSONObject.getString("stepOriginInstruction");
            if (bool) {
                replaceAll2 = string2.replaceAll("终点", "下一途经点");
            } else {
                replaceAll2 = string2.replaceAll("起点", "途经点");
            }
            jSONObject.put("stepOriginInstruction", replaceAll2);
        }
        if (jSONObject.has("stepDestinationInstruction")) {
            String string3 = jSONObject.getString("stepDestinationInstruction");
            if (bool) {
                replaceAll = string3.replaceAll("终点", "下一途经点");
            } else {
                replaceAll = string3.replaceAll("起点", "途经点");
            }
            jSONObject.put("stepDestinationInstruction", replaceAll);
        }
    }

    public static JSONObject mergeServerRoutes(JSONArray jSONArray) {
        String str;
        String str2;
        String str3 = "destinationLocation";
        if (jSONArray == null || jSONArray.length() == 0) {
            return null;
        }
        JSONArray jSONArray2 = new JSONArray();
        try {
            JSONObject jSONObject = new JSONObject();
            int i = 0;
            int i2 = 0;
            int i3 = 0;
            int i4 = 0;
            while (i2 < jSONArray.length()) {
                JSONArray jSONArray3 = jSONArray.getJSONObject(i2).getJSONArray("routes");
                if (jSONArray3 == null || jSONArray3.length() <= 0) {
                    str = str3;
                } else {
                    JSONObject jSONObject2 = jSONArray3.getJSONObject(i);
                    if (!jSONObject.has("originLocation") && jSONObject2.has("originLocation")) {
                        jSONObject.put("originLocation", jSONObject2.get("originLocation"));
                    }
                    if (jSONObject2.has(str3)) {
                        jSONObject.put(str3, jSONObject2.get(str3));
                    }
                    JSONArray jSONArray4 = jSONObject2.getJSONArray("steps");
                    int i5 = 0;
                    while (i5 < jSONArray4.length()) {
                        JSONObject jSONObject3 = jSONArray4.getJSONObject(i5);
                        if (i2 == 0 || i5 != 0) {
                            str2 = str3;
                        } else {
                            str2 = str3;
                            convertInstructions(jSONObject3, false);
                        }
                        if (i2 != jSONArray.length() - 1 && i5 == jSONArray4.length() - 1) {
                            convertInstructions(jSONObject3, true);
                        }
                        jSONArray2.put(jSONObject3);
                        i5++;
                        str3 = str2;
                    }
                    str = str3;
                    i3 += jSONObject2.getInt(MonitoringReader.DISTANCE_STRING);
                    i4 += jSONObject2.getInt("duration");
                }
                i2++;
                str3 = str;
                i = 0;
            }
            jSONObject.put("steps", jSONArray2);
            jSONObject.put(MonitoringReader.DISTANCE_STRING, i3);
            jSONObject.put("duration", i4);
            JSONArray jSONArray5 = new JSONArray();
            jSONArray5.put(jSONObject);
            JSONObject jSONObject4 = new JSONObject();
            jSONObject4.put("origin", jSONArray.getJSONObject(0).get("origin"));
            jSONObject4.put("destination", jSONArray.getJSONObject(jSONArray.length() - 1).get("destination"));
            jSONObject4.put("routes", jSONArray5);
            return jSONObject4;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static JSONObject getCopyright() {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("imageUrl", "http://api.map.baidu.com/images/copyright_logo.png");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jSONObject;
    }
}